<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
function emoji2str($str){     
    $strEncode = '';       
    $length = mb_strlen($str,'utf-8');       
    for ($i=0; $i < $length; $i++) {         
        $_tmpStr = mb_substr($str,$i,1,'utf-8');         
        if(strlen($_tmpStr) >= 4){             
            $strEncode .= '[[EMOJI:'.rawurlencode($_tmpStr);         
        }else{             
            $strEncode .= $_tmpStr;        
        }     
    }     
    return $strEncode; 
}
/* 读数据后重新转换成emoji返回 */ 
function str2emoji($str){     
    $str_arr = explode("[[EMOJI:", $str);     
    $new_str = "";       
    foreach ($str_arr as $key => $val){         
        $new_str .= rawurldecode($val);     
    }       
    return $new_str; 
}
// 获取代理名称
function getDailiName($phone){
    $m['phone'] = $phone;
    $yao = db('yao')->where($m)->find();
    $m2['phone'] = $yao['sj_phone'];
    $admin = db('admin')->where($m2)->find();
    return $admin['nickName'];
}
// 获取设备号
function get_device_type(){
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $type = 'other';
    if(strpos($agent, 'iphone') || strpos($agent, 'ipad') ){
        $type = 'ios';
    }
    if(strpos($agent, 'android')){
        $type = 'android';
    }
    return $type;
}
// 转换IP地址
function zhuanhuanIpAddres($ip){
   header("Content-type: text/html; charset=utf-8");
   $url = "https://api.map.baidu.com/location/ip?ak=ywPxclaARud3wvFiQjG8K7EoIvQoBUQc&ip=".$ip; 
   $res = file_get_contents($url);
   $r = json_decode($res,true);
   return $r['address'];
}


// 转换性别
function conversionSex($str){
    if($str == 'female'){
        return '女';
    }else if($str == 'male'){
        return '男';
    }else{
        return '未知';
    }
}
  // 解除黑名单
  function jiechuUsers($userId){
      $timestamp = time();

      $nonce = rand();
        
      $appSecret = 'rjO7mV5y5BNJny';
        
        $signature = sha1($appSecret.$nonce.$timestamp);
        $date = date('Ymd');
        
         $header  = array(
            'App-Key:'."y745wfm8ydvjv",
            'Nonce:'.$nonce,
            'Timestamp:'.$timestamp,
            'Signature:'.$signature
        );
        $url = "https://api-cn.ronghub.com/user/unblock.json";
       
        $data['userId'] = $userId;
        $r = _curlPost($url,$data,$header);
      
  }
 // 加入黑名单
  function shieldingUsers($userId){
        $timestamp = time();

        $nonce = rand();
        
        $appSecret = 'rjO7mV5y5BNJny';
        
        $signature = sha1($appSecret.$nonce.$timestamp);
        $date = date('Ymd');
        
         $header  = array(
            'App-Key:'."y745wfm8ydvjv",
            'Nonce:'.$nonce,
            'Timestamp:'.$timestamp,
            'Signature:'.$signature
        );
        $url = "https://api-cn.ronghub.com/user/block.json";
       
        $data['userId'] = $userId;
        $data['minute'] = 43200;
        $r = _curlPost($url,$data,$header);
       
    }
 function _curlPost($url, $data, $header = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $info = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Errno' . curl_error($ch);
        }
        curl_close($ch);
        return $info;
    }
    
// 应用公共文件
// 应用公共文件
function echo_info($code, $msg, $count,$data)
{
    $code = array('code' => $code, 'msg' => $msg ,'count'=> $count, 'data'=>$data);
    echo   json_encode($code, JSON_UNESCAPED_UNICODE);
    exit;
}
function randomkeys($length){
    $info="";
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
    for($i=0;$i<$length;$i++) {
        $info .= $pattern{mt_rand(0,35)};    //生成php随机数
    }
    return $info;
}
function send_post($url, $post_data) {
    $headers = ['Content-Type' => "application/x-www-form-urlencoded;charset=utf-8"];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    $sResult = curl_exec($ch);
    if($sError=curl_error($ch)){
        die($sError);
    }
    curl_close($ch);
    return $sResult;
}
function list_file($date){
    //1、首先先读取文件夹
    $temp=scandir($date);
    //遍历文件夹
    $res=array();
    foreach($temp as $v){

        $a=$date.'/'.$v;
        if(is_dir($a)){
        }else{
            //echo $a,"<br/>";
            $res[]=str_replace('../','',$a);
        }

    }
    return $res;
}

// 烟草证认证识别
function authYanchao($STRING){
    $host = "http://tobacoo.market.alicloudapi.com";
    $path = "/icredit_ai_market/tobacco_monopoly/v1";
    $method = "GET";
    //阿里云APPCODE
    $appcode = "c5ffadc3c470446d9f5eaa928e851e64";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    array_push($headers, "Content-Type:application/json; charset=utf-8");

    //参数配置
    //中国烟草专卖零售许可证编号，如：370921106436

    $querys = "STRING=".$STRING;
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    $T = curl_exec($curl);
    return $T;
}